<?php

namespace app\admin\service;

use app\api\service\CommonService;

class AgentServiceConfig extends CommonService
{
    public $agentServType = PRODUCT_SERV_TYPE;

    /*
     保存的时候 参数校验
     todo 设置提交格式
    */
    public function validate($data)
    {
        foreach ($this->agentServType as $k => $v) {
            if (!isset($data[$k])) {
                return $this->inValiReturn('缺少'.$v['name'].'配置项,请刷新页面重试或者联系我们');
            }
            // 状态
            if (!empty($data[$k]['status'])) {
                if (in_array($k, [PRODUCT_MOBILE_SERV, PRODUCT_OILCARD_SERV])) {
                    if ((empty($data[$k]['discount_fast']) || empty($data[$k]['discount_slow']))) {
                        return $this->inValiReturn('请填写'.$this->agentServType[$k]['name'].'快慢充折扣');
                    }
                }
            }
        }

        return $this->valiReturn();
    }

    /*
     把话费配置从配置列表提取到主表字段中
    */
    public function takeMobileConfig(&$config)
    {
        $data = [];
        if (isset($config[PRODUCT_MOBILE_SERV])) {
            // todo
            $data['mobile_discount_fast'] = $config[PRODUCT_MOBILE_SERV]['discount_fast'];
            $data['mobile_discount_slow'] = $config[PRODUCT_MOBILE_SERV]['discount_slow'];
        }

        return $data;
    }
}
